package com.net.oil.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.app.UserInfoManager;
import com.net.oil.base.BaseActivity;
import com.net.oil.base.EventBussMessage;
import com.net.oil.http.AppUrl;
import com.net.oil.main.AboutUsActivity;
import com.net.oil.user.contract.SetContract;
import com.net.oil.user.presenter.SetPresenter;
import com.net.oil.webview.WebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class SetActivity extends BaseActivity<SetPresenter> implements SetContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.safe_layout)
    RelativeLayout safeLayout;
    @BindView(R.id.about_us_layout)
    RelativeLayout aboutUsLayout;
    @BindView(R.id.delete_account_layout)
    RelativeLayout deleteAccountLayout;
    @BindView(R.id.logout_but)
    Button logoutBut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);
        presenter = new SetPresenter(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText(R.string.system_set);

        if (!UserInfoManager.getInstance().isLogin()) {
            logoutBut.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.toolbar_go_back, R.id.safe_layout, R.id.about_us_layout, R.id.logout_but, R.id.delete_account_layout,
            R.id.user_agreement_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.safe_layout:
                startActivity(new Intent(this, ChangeLoginPasswordActivity.class));
                break;
            case R.id.about_us_layout:
                startActivity(new Intent(this, AboutUsActivity.class));
                break;
            case R.id.logout_but:
                logout();
                break;
            case R.id.delete_account_layout:
                startActivity(new Intent(this, DeleteAccountActivity.class));
                break;
            case R.id.user_agreement_layout:
                Intent intent = new Intent();
                intent.setClass(this, WebViewActivity.class);
                intent.putExtra(Constant.INTENT_TO_WEBVIEW_URL_KEY, AppUrl.ABOUS_US);
                intent.putExtra(Constant.INTENT_IS_SHARE, false);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void logout() {
        presenter.logout();


    }

    /**
     * 退出登录成功
     */
    @Override
    public void logoutSuccess() {
        dismissProgressDialog();
        setResult(RESULT_OK);
        EventBus.getDefault().post(new EventBussMessage(Constant.LOGOUT_CODE, Constant.LOGOUT_MESSAGE));
        finish();
        UserInfoManager.getInstance().clearUserInfo();
    }

    /**
     * 退出登录失败（成功失败都清楚用户信息）
     */
    @Override
    public void logoutFailed() {
        dismissProgressDialog();
        setResult(RESULT_OK);
        EventBus.getDefault().post(new EventBussMessage(Constant.LOGOUT_CODE, Constant.LOGOUT_MESSAGE));
        finish();
        UserInfoManager.getInstance().clearUserInfo();
    }
}
